<template>
  <el-dialog
    title="产品BOM物料选择"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal="false"
    width="60%"
    center
    :close-on-click-modal ="false"
  >
    <!-- <el-form
      :model="queryParams"
      ref="queryParams"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="物料编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="物料名称" prop="sonname">
        <el-input
          v-model="queryParams.sonname"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>-->
    <div class="per">
      <el-table
        v-loading="loading"
        :data="bomList"
        @current-change="handleCurrent"
        @row-dblclick="handleRowDbClick"
        @selection-change="handleSelectionChange"
        max-height="620"
        row-key="bomItemCode"
        :row-class-name="tableRowClassName"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" width="50px">
          <template slot-scope="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <!--      <el-table-column width="50" align="center">-->
        <!--        <template v-slot="scope">-->
        <!--          <el-radio v-model="selectedItemId" :label="scope.row.bomItemId" @change="handleRowChange(scope.row)">-->
        <!--            {{ "" }}-->
        <!--          </el-radio>-->
        <!--        </template>-->
        <!--      </el-table-column>-->
        <el-table-column label="物料编码" align="center" prop="itemCode" />
        <el-table-column label="物料名称" align="center" prop="sonname" />
        <el-table-column label="规格" align="center" prop="specification" />

        <el-table-column label="用量:分子" align="center" show-overflow-tooltip prop="jdyNumerator"></el-table-column>

        <el-table-column label="用量:分母" width="90px" align="center" prop="jdyDenominator">
          <template slot-scope="scope">
            <span>{{ scope.row.jdyDenominator ? scope.row.jdyDenominator : 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="单位" align="center" prop="measureName" />
        <!-- <el-table-column label="使用比例" align="center" prop="quantity"/> -->
        <!-- <el-table-column
        label="备注"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
        />-->
      </el-table>
    </div>
    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />-->
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listBom, routeBOMList } from "@/api/mes/md/bom";
import {
  addRouteproductbom,
  updateRouteproductbom
} from "@/api/mes/pro/routeproductbom";

export default {
  name: "ItemBomSelectSingle",
  props: {
    itemId: undefined,
    routeId: undefined,
    processId: undefined,
    processCode: undefined,
    processName: undefined
  },
  data() {
    return {
      showFlag: false,
      // 遮罩层
      loading: true,
      selectedItemId: undefined,
      selectedRows: undefined,
      selectedData: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品BOM关系表格数据
      bomList: [],
      //   选中信息
      checkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        // productId: this.itemId,
        pageNum: 1,
        pageSize: 999,
        // itemId: this.itemId,
        workorderCode: null,
        bomId: null,
        jdyItemId: this.jdyItemId,
        bomItemId: null,
        bomItemCode: null,
        bomItemName: null,
        bomItemSpec: null,
        unitOfMeasure: null,
        quantity: null,
        enableFlag: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null
      },
      //新增确定后保存的id数组
      Ids: [],
      //回显的list
      list: [],
      form: {}
    };
  },
  created() {
    // this.getList();
  },
  methods: {
    init(row, repetitive, data, obj) {
      console.log(row, "回显的row");
      this.list = row || [];
      this.repetitive = repetitive;
      this.form = { ...data, ...obj };
      this.queryParams.workorderCode = this.form.workorderCode;
      this.queryParams.bomId = this.form.bomId;
      this.getList();
    },
    /** 查询产品BOM关系列表 */
    getList() {
      //debugger;
      this.loading = true;
      routeBOMList(this.queryParams).then(response => {
        debugger
        console.info("response.rows====bom====>", response);
        //add begin by zyj 2024/10/17 未选物料放列表最上面
        let nonSelectedData = response.data.filter(item => this.list.filter(listItem=>listItem.itemCode==item.itemCode).length == 0);

        let selectedData = response.data.filter(item=>this.list.filter(listItem=>listItem.itemCode==item.itemCode).length > 0);
        
        response.data = nonSelectedData.concat(selectedData);
        //add end by zyj 2024/10/17 未选物料放列表最上面
        this.bomList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    //搜索
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryParams");
      this.handleQuery();
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    handleRowDbClick(row) {
      if (row) {
        this.selectedRows = row;
        this.$emit("onSelected", this.selectedRows);
        this.showFlag = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.info("selection===", selection);
      console.log(this.repetitive, "=====this.repetitive======");
      this.selectedData = selection;
      this.checkList = selection;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.ids = selection.map(item => item.itemCode);
      if (this.repetitive) {
        let list = this.repetitive.map(item => item.itemCode);
        let repetitivea = this.compare(list, this.ids);
        console.log(repetitivea, "之前提交了 现在又选了的");
        if (repetitivea.length != 0) {
          this.$modal.msgError("产品物料编码为" + repetitivea + "已被选择");
        }
      }
    },

    //确定选中
    confirmSelect() {
      if (this.ids == null || this.ids == 0) {
        this.$notify({
          title: "提示",
          type: "warning",
          message: "请至少选择一条数据!"
        });
        return;
      } else {
        console.log( this.checkList,this.repetitive,"=======================1111");
        this.ids = this.checkList.map(item => item.itemCode);
        let repetitivea;
        if (this.repetitive ) {
          let list = this.repetitive.map(item => item.itemCode);
          repetitivea = this.compare(list, this.ids);
        }

        console.log(repetitivea, "之前提交了 现在又选了的");
        if (repetitivea && repetitivea.length != 0) {
          this.$modal.msgError("产品物料编码为" + repetitivea + "已被选择");
          return;
        } else {
          this.showFlag = false;
          console.log(this.form, "=======this.form=======");
          this.selectedData.forEach(obj => {
            obj.processId = this.form.processId;
            obj.processCode = this.form.processCode;
            obj.processName = this.form.processName;
            obj.routeId = this.form.routeId;
            obj.productId = this.form.itemId;
            obj.bomItemCode = this.form.bomItemCode;
            obj.workorderId = this.form.workorderId;
            obj.workorderCode = this.form.workorderCode;
            obj.productId = obj.itemId;
            obj.itemId = obj.bomItemId;
            obj.itemName = obj.sonname;
          });
          console.log(this.selectedData, "=====selectdata=============");
          addRouteproductbom(this.selectedData).then(response => {
            console.info("response.rows====bom====>", response);
            this.open = false;
            this.ids = null;
            this.selectedData = [];
            this.$emit("backEndData");
          });
        }
      }
    },
    compare(arr1, arr2) {
      return arr1.filter(v => {
        return arr2.includes(v);
      });
    },
    //改变颜色
    tableRowClassName({ row, rowIndex }) {
      // console.log(row, "row");
      console.log(this.list, "this.list");
      let bool = this.list.filter(_ => _.itemCode == row.itemCode);
      if (bool && bool.length > 0) {
        return "success-row";
      }
      return "";
    }
  }
};
</script>
<style scoped>
.per ::v-deep .el-table .warning-row {
  background: oldlace;
}
.per ::v-deep .el-table .success-row {
  background: rgba(107, 178, 241, 0.722);
}
.per ::v-deep .el-table tbody tr:hover > td {
  background-color: unset !important;
}
</style>
